﻿using System;
using System.Collections.Generic;
using System.Linq;
using LDM.Sys.Repository.Interfaces;
using LDM.Sys.ServiceAPI.Model.DBModel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Model.Enum.ConfigModel;
using Utility.DB.SqlSugar;
using Utility.Extensions;

namespace LDM.Sys.Repository.Repository
{
    /// <summary>
    /// 菜单仓储
    /// </summary>
    public class SysMenuRepository : DbContextBase<SysMenu>, ISysMenuRepository, IScoped
    {
        public SysMenuRepository(IConfiguration configuration) :
            base(configuration.GetConnectionString(ConnectionStringType.UserMysql.GetDescription()))
        {
        }

        /// <summary>
        /// 获取所有菜单数据
        /// </summary>
        /// <returns></returns>
        public List<SysMenu> GetSysMenus()
        {
            return Db.Queryable<SysMenu>().ToTree(it => it.ChildMenu, it => it.PId, 0);
        }


        /// <summary>
        /// 获取当前菜单Id下的所有父级菜单Id
        /// </summary>
        /// <returns></returns>
        public List<SysMenu> GetParentMenus(int menuId)
        {
            return Db.Queryable<SysMenu>().ToParentList(it => it.PId, menuId);
        }

        /// <summary>
        /// 获取当前菜单Id下的所有子级菜单Id
        /// </summary>
        /// <returns></returns>
        public List<SysMenu> GetChildMenus(int menuId)
        {
            return Db.Queryable<SysMenu>().ToChildList(it => it.PId, menuId); 
        }
    }
}
